package com.kzj.laimeimei.recyclerCard.cardView;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.kzj.laimeimei.R;
import com.kzj.laimeimei.beans.BookDateTime;
import com.kzj.laimeimei.recyclerCard.basic.CardItemView;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListAdapter;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.ChooseRightTimeCard;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/9.
 */
public class ChooseTimeRightCardView extends CardItemView<ChooseRightTimeCard> {

    private Context context;
    private TextView textView_time;
    private MaterialListView materialListViewTime;
    /*private TextView textView_title;*/

    public ChooseTimeRightCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ChooseTimeRightCardView(Context context) {
        super(context);
        this.context = context;
    }

    public ChooseTimeRightCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void build(final ChooseRightTimeCard card) {
        super.build(card);
        textView_time = (TextView) findViewById(R.id.textView_time);
        textView_time.setText(card.getTimeBean().getTime());
        materialListViewTime = card.getMaterialListView();
        //先判断booktime
        if (card.getBookDateTime()!=null && !TextUtils.isEmpty(card.getBookDateTime().getBooking_time()) &&card.getBookDateTime().getBooking_time().equals(card.getTimeBean().getTime()))
        {
            textView_time.setBackgroundColor(getResources().getColor(R.color.color_f875a3_red));
            textView_time.setEnabled(false);
        }
        else if(card.getServiceTimeEntity()!=null && card.getServiceTimeEntity().getIs_use().equals("false"))
        {
            textView_time.setBackgroundColor(getResources().getColor(R.color.color_cccccc_gray));
            textView_time.setEnabled(false);
        }
        else
        {
            textView_time.setEnabled(true);
            textView_time.setBackgroundColor(getResources().getColor(R.color.white));
            textView_time.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    BookDateTime bookDateTime = card.getBookDateTime();
                    bookDateTime.setBooking_time(textView_time.getText().toString());
                    EventBus.getDefault().post(bookDateTime);

                    for (int i = 0; i < materialListViewTime.getAdapter().getItemCount(); i++) {
                        ChooseRightTimeCard chooseRightTimeCard = (ChooseRightTimeCard) ((MaterialListAdapter) materialListViewTime.getAdapter()).getCard(i);
                        chooseRightTimeCard.setIsSelected(false);
                    }

                    card.setIsSelected(true);
                    materialListViewTime.getAdapter().notifyDataSetChanged();

                }
            });
        }


    }
}
